﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Heater.cs" company="Jarno Burger">
//   Jarno Burger
// </copyright>
// <summary>
//   This controls the heating switch.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MyDomotica
{
    using System;

    using MyDomotica.Model;

    /// <summary>
    /// This controls the heating switch.
    /// </summary>
    public class Heater
    {
        #region Declare
        /// <summary>
        /// This tells that this class has a debug event.
        /// </summary>
        public EventHandler<string> EventDebug;

        /// <summary>
        /// The model. This contains all the actual data.
        /// </summary>
        private readonly Model.Model model;
        #endregion

        #region Initialize
        /// <summary>
        /// Initializes a new instance of the <see cref="Heater"/> class.
        /// </summary>
        /// <param name="model">The model.</param>
        public Heater(Model.Model model)
        {
            this.model = model;
            this.Debug("Heater object is created.");
        }
        #endregion

        #region Update

        /// <summary>
        /// Updates this instance.
        /// </summary>
        public void Update()
        {
            // get the preferred Temperature by checking which time of day it is.
            var now = DateTime.Now;
            int preferredTemperature;
            if (now.Hour > Constants.BeginOfMorning && now.Hour < Constants.EndOfMiddayHour)
            {
                preferredTemperature = Constants.PreferredDayTemperature;
            }
            else
            {
                preferredTemperature = Constants.PreferredNightTemperature;
            }
            
            this.model.CurrentPreferredTemperature = preferredTemperature;
            
            // turn the heater on or off by checking current and preferred temperature.
            var currentTemperature = this.model.CurrentTemperature;
            if (currentTemperature < preferredTemperature)
            {
                this.Debug("Turning heater on.");
                this.model.CurrentHeaterIsOn = true;
            }
            else
            {
                this.Debug("Turning heater off.");
                this.model.CurrentHeaterIsOn = false;
            }
        }
        #endregion

        #region Debug
        /// <summary>
        /// Creates a debug event out of the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        private void Debug(string message)
        {
            if (this.EventDebug != null)
            {
                this.EventDebug(this, message);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine(message);
            }
        }
        #endregion
    }
}
